<template>
	<view>
		<view class="goods-card" @click="goDetail(goods._id)">
			<view class="goods-img">
				<img :src="serverUrl + goods.img_url[0]"></image>
			</view>
			<view class="goods-main">
				<view class="goods-title">
					{{goods.name}}
				</view>
				<view class="font-hidden color-gray">
					材料：{{goods.materials}}（款式和尺寸均可定制，详情请咨询客服）
				</view>
				<view class="color-gray">1007人已付款</view>
				<view style="color: #f00;font-size: 35rpx">
					￥{{goods.price}}<text class="color-gray" v-show="isShow"
						style="text-decoration: line-through;">￥{{goods.pre_price}}</text>
				</view>
				<view class="goods-cart" @click.stop="openPopup">
					<uni-icons type="cart" size="30" color="#f00"></uni-icons>
				</view>
			</view>
		</view>
		<!-- 底部弹出框 -->
		<uni-popup ref="popup" type="bottom" @maskClick="clearData">
			<add-cart :goodsData="goodsData" :active="active" @handleSize='handleActive' @addCartOk="addCartOk">
			</add-cart>
		</uni-popup>
	</view>
</template>

<script>
	export default {
		name: "goods-card",
		props: {
			goods: {
				type: Object,
				default: {},
			},
			isShow: {
				type: Boolean,
				default: false,
			}
		},
		data() {
			return {
				goodsData: {},
				active: -1,
			};
		},
		methods: {
			// 跳转至商品详情页
			goDetail(goodsId) {
				uni.navigateTo({
					url: `/subpkg/goods-detail/goods-detail?goodsId=${goodsId}`
				})
			},
			// 点击打开底部弹出框
			openPopup() {
				this.goodsData = this.goods;
				this.$refs.popup.open('bottom')
			},
			// 点击遮罩层清空弹出层所需数据
			clearData() {
				this.goodsData = {};
				this.active = -1;
			},
			handleActive(i) {
				this.active = this.active == i ? -1 : i;
			},
			// 商品加购成功
			addCartOk() {
				uni.showToast({
					title: '已将商品加入购物车',
					icon: 'none',
				})
				this.clearData();
				this.$refs.popup.close();
			}
		}
	}
</script>

<style lang="scss">
	.goods-card {
		display: flex;
		box-sizing: border-box;
		padding: 15rpx 40rpx 25rpx 15rpx;
		border-radius: 20rpx;
		background-color: #fff;
		overflow: hidden;

		.goods-img {


			img {
				width: 200rpx;
				height: 200rpx;
			}
		}

		.goods-main {
			position: relative;
			flex: 1;
			display: flex;
			flex-direction: column;
			justify-content: space-between;
			overflow: hidden;
			padding-left: 30rpx;

			.goods-title {
				font-size: 35rpx;
				font-weight: 700;
			}

			.color-gray {
				color: #666;
				font-size: 28rpx;
			}

			.font-hidden,
			.goods-title {
				white-space: nowrap;
				overflow: hidden;
				text-overflow: ellipsis;
			}

			.goods-cart {
				position: absolute;
				right: 0;
				bottom: 0;
			}
		}
	}
</style>
